export default {
    namespaced: true,
    state: {//仓库
        cart: [],
        isInit: false,
    },
    //计算
    getters: {
        length(state){
            return state.cart.length;
        },
        product(state){
            let product = {};
            let total = 0;
            let num = 0;
            for(let i = 0; i < state.cart.length; i++){
                if(state.cart[i].activate){
                    total += state.cart[i].num * state.cart[i].price;
                    num++;
                }
            }
            product.total = total;
            product.num = num;
            return product;
        }
    },
    //更改仓库
    mutations: {
        _init(state, payload){
            state.isInit = true;
            for(let i = 0; i < payload.length; i++){
                payload[i].activate = true;
            }
            state.cart = payload;
        },
        toggle(state, payload){
            for(let i = 0; i < state.cart.length; i++){
                if(payload.id === state.cart[i].id) state.cart[i].activate = !state.cart[i].activate;
            }
        },
        _del(state, payload){
            for(let i = 0; i < state.cart.length; i++){
                if(payload.id === state.cart[i].id) state.cart.splice(i, 1);
                break;
            }
        },
    },
    //异步请求
    actions: {
        init({commit, rootState: {$http}}, payload){
            $http.post("/cart/init", {user: payload})
                .then(result => commit("_init", result.data));
        },
        add({commit, rootState: {$http}}, payload){
            $http.post("/cart/add", {id: payload.id, num: payload.num += 1})
                .then(result => {
                });
        },
        sub({commit, rootState: {$http}}, payload){
            $http.post("/cart/sub", {id: payload.id, num: payload.num -= 1})
                .then(result => {
                });
        },
        del({commit, rootState: {$http}}, payload){
            $http.post("/cart/del", {id: payload.id})
                .then(result => commit("_del", payload));
        },
        //商品详情添加到购物车
        addProduct({dispatch, commit, state, rootState: {$http}}, payload){
            let isAdd = true;
            if(state.cart.length){
                for(let i = 0; i < state.cart.length; i++){
                    if(state.cart[i].name === payload.name && state.cart[i].price === payload.price){
                        dispatch("add", state.cart[i]);
                        isAdd = false;
                        break;
                    }
                }
            }
            if(!isAdd && state.cart.length < 1){
                $http.post("/cart/add", {data: payload})//路由和传参
                    .then(result => {
                        dispatch("init", payload.user);
                    });
            }
            if(isAdd){
                $http.post("/cart/add", {data: payload})//路由和传参
                    .then(result => {
                        dispatch("init", payload.user);
                    });
            }
        },
    }
};